﻿using Common.Communal;
using ERP.Domain.Scheduling;
using Scheduling.Repository.Interface;

namespace Scheduling.Services.Shifts
{
    public class ShiftServices : IShiftServices
    {
        public ShiftServices(IShiftRepository shiftRepository)
        {
            ShiftRepository = shiftRepository;
        }

        public IShiftRepository ShiftRepository { get; }

        public async Task<ApiResult<int>> AddShift(Shift shift)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                if (shift == null || string.IsNullOrWhiteSpace(shift.ShiftName))
                {
                    result.Code = EnumCode.Fail;
                    result.Message = "班次信息无效";
                    return result;
                }

                var affected = await ShiftRepository.AddAsync(shift);
                result.Code = EnumCode.Ok;
                result.Message = "班次添加成功";
                result.Data = affected;
            }
            catch (Exception ex)
            {
                result.Code = EnumCode.Fail;
                result.Message = $"添加失败：{ex.Message}";
            }
            return await Task.FromResult(result);
        }

        public async Task<ApiResult<int>> UpdShift(Shift shift)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                if (shift == null || shift.ShiftId <= 0)
                {
                    result.Code = EnumCode.Fail;
                    result.Message = "班次ID无效";
                    return result;
                }

                var existing = await ShiftRepository.GetModelAsync(shift.ShiftId);
                if (existing == null)
                {
                    result.Code = EnumCode.Fail;
                    result.Message = "班次不存在";
                    return result;
                }

                existing.ShiftName = shift.ShiftName;
                existing.StartTime = shift.StartTime;
                existing.EndTime = shift.EndTime;
                existing.IsNightShift = shift.IsNightShift;
                existing.DisplayColor = shift.DisplayColor;
                existing.OvertimeRate = shift.OvertimeRate;
                existing.Breakduration = shift.Breakduration;
                existing.ShiftStatus = shift.ShiftStatus;

                var affected = await ShiftRepository.Update(existing);
                result.Code = EnumCode.Ok;
                result.Message = "班次修改成功";
                result.Data = affected;
            }
            catch (Exception ex)
            {
                result.Code = EnumCode.Fail;
                result.Message = $"修改失败：{ex.Message}";
            }
            return await Task.FromResult(result);
        }

        public async Task<ApiResult<int>> DeleteShift(int shiftId)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                if (shiftId <= 0)
                {
                    result.Code = EnumCode.Fail;
                    result.Message = "班次ID无效";
                    return result;
                }

                var existing = await ShiftRepository.GetModelAsync(shiftId);
                if (existing == null)
                {
                    result.Code = EnumCode.Fail;
                    result.Message = "班次不存在";
                    return result;
                }

                existing.IsDelete = true;
                var affected = await ShiftRepository.Update(existing);
                result.Code = EnumCode.Ok;
                result.Message = "班次删除成功";
                result.Data = affected;
            }
            catch (Exception ex)
            {
                result.Code = EnumCode.Fail;
                result.Message = $"删除失败：{ex.Message}";
            }
            return await Task.FromResult(result);
        }
    }
}
